import json
from py2neo import Graph, Node, Relationship, NodeMatcher


def load_json(path):
    with open(path, 'r', encoding='utf-8') as f:
        data = json.load(f)
    return data


def find_code(entity_name, graph_name='Naruto'):
    find_code_1 = matcher.match(graph_name, name=entity_name).first()
    if not find_code_1:
        node_1 = Node(graph_name, name=entity_name)
        neo_graph.create(node_1)
        return node_1
    else:
        return find_code_1

if __name__ == '__main__':
    data = load_json(path)
    neo_graph = Graph("http://localhost:7474",
                      auth=("neo4j", "your passwords"))
    matcher = NodeMatcher(neo_graph)
    for each_data in data:
        entity_1 = each_data['entity_1']
        entity_2 = each_data['entity_2']
        relationship = each_data['relationship']
        node_1 = find_code(entity_1)
        node_2 = find_code(entity_2)
        node_1_call_node_2 = Relationship(node_1, relationship, node_2)
        neo_graph.create(node_1_call_node_2)
